home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / PowerD / alpha / examples / bintree.d < prev    next >
Encoding:
Text File  |  2002-10-28  |  996 b   |  50 lines

  1. OPT OPTIMIZE=3,DOSONLY
  2.  
  3. OBJECT tree
  4.   data,
  5.   left:PTR TO tree, right:PTR TO tree
  6.  
  7. PROC new_set(int)(PTR TO tree)
  8.   DEF root:PTR TO tree
  9.   NEW root
  10.   root.data:=int
  11. ENDPROC root
  12.  
  13. PROC add(i, set:PTR TO tree)(PTR TO tree)
  14.   IF set=NIL
  15.     RETURN new_set(i)
  16.   ELSE
  17.     IF i<set.data
  18.       set.left:=add(i, set.left)
  19.     ELSEIF i>set.data
  20.       set.right:=add(i, set.right)
  21.     ENDIF
  22.     RETURN set
  23.   ENDIF
  24. ENDPROC
  25.  
  26. PROC show(set:PTR TO tree)
  27.   IFN set=NIL
  28.     show(set.left)
  29.     PrintF('\d ', set.data)
  30.     show(set.right)
  31.   ENDIF
  32. ENDPROC
  33.  
  34. PROC main()
  35.   DEF s, i, j
  36.   Rnd(-99999)    /* Initialise seed */
  37.   s:=new_set(10)  /* Initialise set s to contain the number 10 */
  38.   PrintF('Input:\n')
  39.   FOR i:=1 TO 50  /* Generate 50 random numbers and add them to set s */
  40.     j:=Rnd(100)
  41.     add(j, s)
  42.     PrintF('\d ',j)
  43.   ENDFOR
  44.   PrintF('\nOutput:\n')
  45.   show(s)         /* Show the contents of the (sorted) set s */
  46.   PrintF('\n')
  47. EXCEPT
  48.   IF exception="NEW" THEN PrintF('Ran out of memory\n')
  49. ENDPROC
  50.